package org.springframework.data.jpa.repository.query;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.util.Assert;

import basic.com.liz.common.log.util.PropertyLogUtils;

 

public class CommonModifyExecution extends JpaQueryExecution
{
    private final EntityManager em;
    
    public CommonModifyExecution(JpaQueryMethod method, EntityManager em)
    {
        Class<?> returnType = method.getReturnType();
        
        boolean isVoid = void.class.equals(returnType) || Void.class.equals(returnType);
        boolean isInt = int.class.equals(returnType) || Integer.class.equals(returnType);
        
        Assert.isTrue(isInt || isVoid, "Modifying queries can only use void or int/Integer as return type!");
        
        this.em = em;
    }
    
    @Override
    protected Object doExecute(AbstractJpaQuery query, Object[] values)
    {
       
        Query executeQuery = query.createQuery(values);
        int result = executeQuery.executeUpdate();
        
        PropertyLogUtils.processExecuteUpdate((org.hibernate.ejb.QueryImpl<?>)executeQuery);
        
        if (em != null)
        {
            em.clear();
        }
        
        return result;
    }
}
